#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <unistd.h>
#include <iostream>

#include "rabbitMQUtil.h"
#include "IRabbitMQUtil.h"
#include <thread>
#include <deque>
#include <chrono>


using namespace std;

std::string vecRecvMsg;

extern bool bhave;

void RecvDataByqueue(IRabbitMQUtil* client, std::deque<std::string> *message_queue)
{
    printf(__FUNCTION__);
    std::string strQueuename = "rabbitMQUtil.queue.test";
    while (true)
    {
        if (!message_queue->empty())
        {
            printf("m_message_queue is not empty");
            std::string sRet = message_queue->front();
            message_queue->pop_front();
            printf(sRet.c_str());
            std::cout << sRet;
        }
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    
    //int iRet = client->Consumer(strQueuename, vecRecvMsg, 1);
    //printf("Rabbitmq Consumer Ret: %d\n", iRet);

    //client->Disconnect();
}

//void RecvData(RabbitMQUtil* client)
//{
//    printf(__FUNCTION__);
//    std::string strQueuename = "rabbitMQUtil.queue.test";
//
//    
//    int iRet = client->Consumer(strQueuename, vecRecvMsg, 1);
//    printf("Rabbitmq Consumer Ret: %d\n", iRet);
//
//    
//
//    client->Disconnect();
//}

void CoutData()
{
    printf(__FUNCTION__);
    while (1)
    {
        if (bhave == true)
        {
            
                printf("Consumer: %s\n", vecRecvMsg.c_str());
            

            bhave = false;
        }
        
    }
}

int main()
{
    //RabbitMQUtil objRabbitmq;
    int iRet;
    std::string strIP = "127.0.0.1";
    int iPort = 5672;
    std::string strUser = "guest";
    std::string strPasswd = "guest";
    std::string strExchange = "ExchangeTest2";
    std::string strQueuename = "rabbitMQUtil.queue.test";
    std::string strRoutekey = "routekeyTest";
    std::deque<std::string> message_queue;
    message_queue.clear();

    IRabbitMQUtil *objRabbitmq = GetMQ(strIP, iPort, strUser, strPasswd, strExchange, strQueuename, strRoutekey, &message_queue);

    //int iRet = objRabbitmq.Connect(strIP, iPort, strUser, strPasswd);
    //printf("Rabbitmq Connect Ret: %d\n", iRet);

    ////// 可选操作 Declare Exchange
    //iRet = objRabbitmq.ExchangeDeclare(strExchange, "direct");
    //printf("Rabbitmq ExchangeDeclare Ret: %d\n", iRet);

    ////// 可选操作（接收） Declare Queue
    //iRet = objRabbitmq.QueueDeclare(strQueuename);
    //printf("Rabbitmq QueueDeclare Ret: %d\n", iRet);

    ////// 可选操作（接收） Queue Bind
    //iRet = objRabbitmq.QueueBind(strQueuename, strExchange, strRoutekey);
    //printf("Rabbitmq QueueBind Ret: %d\n", iRet);

    //// Send Msg
    //std::string strSendMsg1 = "rabbitmq send test msg1";
    //std::string strSendMsg2 = "rabbitmq send test msg2";
    //iRet = objRabbitmq.Publish(strSendMsg1, strExchange, strRoutekey);
    //printf("Rabbitmq Publish 1 Ret: %d\n", iRet);
    //iRet = objRabbitmq.Publish(strSendMsg2, strExchange, strRoutekey);
    //printf("Rabbitmq Publish 2 Ret: %d\n", iRet);

    // Recv Msg
    

    //thread t1(RecvData, &objRabbitmq);
    //t1.detach();
    //t1.join();

    //thread t2(CoutData);
    //t2.detach();
    //t2.join();


    thread t3(RecvDataByqueue, objRabbitmq, &message_queue);
    t3.detach();
    //t3.join();

    //while(1) {}

    // Send Msg
    //std::string strSendMsg1 = "rabbitmq send test msg1";
    //std::string strSendMsg2 = "rabbitmq send test msg2";
    //iRet = objRabbitmq->Publish(strSendMsg1, strExchange, strRoutekey, 2);
    //printf("Rabbitmq Publish 1 Ret: %d\n", iRet);
    //iRet = objRabbitmq->Publish(strSendMsg2, strExchange, strRoutekey, 2);
    //printf("Rabbitmq Publish 2 Ret: %d\n", iRet);

    std::cout << "main end!!!" << endl;

    std::this_thread::sleep_for(std::chrono::seconds(10));
    objRabbitmq->Disconnect();
    return 0;
}